/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.model.dao.impl;

import com.javacommunity.model.dao.PostVotationDAO;
import com.javacommunity.model.entity.Post;
import com.javacommunity.model.entity.User;
import com.javacommunity.model.entity.UserPost;
import com.javacommunity.hibernate.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Cong Pham Dinh
 */
public class PostVotationDAOImpl implements PostVotationDAO {

    private static PostVotationDAOImpl postVotationDAO;

    public static PostVotationDAOImpl getInstance() {
        if (postVotationDAO == null) {
            postVotationDAO = new PostVotationDAOImpl();
        }
        return postVotationDAO;
    }

    private Session session = null;
    private Transaction tx = null;

    @Override
    public boolean isVote(User user, Post post) {
        boolean isCheck = false;

        session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            String sql = "FROM UserPost as u WHERE u.user = ? and u.post=?";
            Query query = session.createQuery(sql);
            query.setParameter(0, user);
            query.setParameter(1, post);
            if (query.list() != null && query.list().size() > 0) {
                isCheck = true;
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            isCheck = false;
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return isCheck;
    }

    @Override
    public boolean updateVote(UserPost up) {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.update(up);
            tx.commit();
            return true;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean createVote(UserPost up) {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.save(up);
            tx.commit();
            return true;

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @Override
    public int countVotation(Post post) {
        int count = 0;
        Transaction tx = null;
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            int countVoteUp = session
                    .createQuery("From UserPost up Where up.post = ? and up.vote = true")
                    .setParameter(0, post).list().size();
            int countVoteDown = session
                    .createQuery("From UserPost up Where up.post = ? and up.vote = false")
                    .setParameter(0, post).list().size();
            tx.commit();
            count = countVoteUp - countVoteDown;
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return count;
    }

    @Override
    public UserPost getUserPostByUP(User user, Post post) {
        UserPost up;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            up = (UserPost) session.createQuery("FROM UserPost up WHERE up.user = ? and up.post = ?")
                    .setParameter(0, user).setParameter(1, post).uniqueResult();
            tx.commit();
            return up;

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            return null;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

}
